package threads;

import java.util.Arrays;

public class Bank {
	private final double[] accounts;
	/**
	 * Constructs the bank
	 *
	 * @param n the number of accounts
	 * @param initialBalance the initial balance for each account
	 */
	public Bank(int n,double initialBalance){
		accounts=new double[n];
		Arrays.fill(accounts,initialBalance);
	}
	public synchronized void transfer(int from,int to,double amount){
		if(accounts[from]<amount)return;
		System.out.print(Thread.currentThread());
		accounts[from]-=amount;
		System.out.printf("%10.2f from %d to %d\t",amount,from,to);
		accounts[to]+=amount;
		System.out.printf("Total Balance: %10.2f%n", getTotalBalance());
	}
	public double getTotalBalance(){
		double sum=0;
		for(double account:accounts)
			sum+=account;
		return sum;
	}

	public int size(){
		return accounts.length;
	}
}
